﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC_Uthorization.API.Application.Command.SysCommand;
using RBAC_Uthorization.API.DTO;
using RBAC_Uthorization.API.DTO.Request;
using RBAC_Uthorization.Domain;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace RBAC_Uthorization.API.Controllers
{
    [Route("api/[controller][action]")]
    [Authorize]
    [ApiController]
    public class SysController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<SysController> _logger;

        public SysController(IMediator mediator, ILogger<SysController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }
        /// <summary>
        /// 新增系统
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> SysCreate(SysCreateDto dto)
        {
            try
            {
                var entity = await _mediator.Send(new SysCreateCommand()
                {
                    dto = dto,
                    CreateName = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value,
                   
                });
                return entity;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "新增系统异常");
                return new ApiResult<int>() { Code = ResultCode.Fail, Message = "新增系统异常" };
            }
        }
    }
}
